﻿(function ($) {
    var responseType = {
        success: 0,
        error: 1
    };

    var methods = {
        init: function (options) {
            this.each(function () {
                //if ($.isFunction(window.__doPostBack)) {
                //    window.__doPostBack = methods.doPostBack;
                //}

                //options.form.submit(methods.submit);
            });
        },
        destroy: function () {
        },
        submit: function (event) {
            return false;
        },
        doPostBack: function (target, argument) {
            var data = $(window).data("scriptManager-data");

            var postData = methods.preparePostBackData(target, argument);

            $.ajax({
                type: data.form.attr("method"),
                url: data.form.attr("action"),
                dataType: "json",
                data: postData
            })
            .done(function (response) {
                //TODO: Handle scripts token
                switch (response.responseType) {
                    case responseType.success:
                        $("#" + response.form.name)
                            .attr("action", response.form.action)
                            .attr("method", response.form.method);

                        $.each(response.hiddenFields, function (index, item) {
                            var value = $("#" + item.name).val();
                            if (value != item.value) {
                                $("#" + item.name).val(item.value);
                            }
                        });

                        $.each(response.updatePanels, function (index, item) {
                            var updatePanel = $("#" + item.updatePanel.name);

                            if (updatePanel.length > 0) {
                                updatePanel.html(item.updatePanel.html);
                            }
                        });

                        $.each(response.scripts, function (index, item) {
                            if (item.script.src) {
                                $("<script></script>")
                                    .attr("src", item.script.src)
                                    .attr("type", item.script.type)
                                    .appendTo("head");
                            }
                            else if (item.script.text) {
                                $("<script></script>")
                                    .attr("language", item.script.language)
                                    .attr("type", item.script.type)
                                    .text(item.script.text)
                                    .appendTo("#scriptsWrapper");
                            }
                        });

                        break;
                    case responseType.error:
                        break;
                }
            })
            .fail(function (response, textStatus, errorThrown) {
                //TODO: Handle error when happends
            });
        },
        preparePostBackData: function (target, argument) {
            var postData = {};

            $("#__ISASYNCPOSTBACK").val("true");
            $("#__EVENTTARGET").val(target);
            $("#__EVENTARGUMENT").val(argument);

            $("input[type!='file'], select, textarea").each(function () {
                postData[$(this).attr("name")] = $(this).val();
            });

            return postData;
        }
    };

    $.fn.pageRequestManager = function (method) {
        if (methods[method]) {
            return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
        } else if (typeof method === 'object' || !method) {
            return methods.init.apply(this, arguments);
        } else {
            $.error('Method ' + method + ' does not exist on jQuery.pageRequestManager');
        }
    };
})(jQuery);